/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import DTO.ListPlaces;
import DTO.PlaceInList;
import Utility.XMLUtils;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author SQL
 */
public class ListPlaceSearch extends HttpServlet {

    ListPlaces listPlaces;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            listPlaces = new ListPlaces();
            String searchText = request.getParameter("txtSearch");
            String searchFile = request.getParameter("txtXMLFile");
            String resultFile = request.getParameter("txtResultFile");
            String realPath = getServletContext().getRealPath("/");
            Document doc = XMLUtils.parseDOMFromFile(realPath + searchFile);
            if (searchText.length() > 0) {
                searchDOM(doc.getLastChild(), searchText);
            }
            // Get the last child, which is the ListPlaces
            // Marshall to file                        
            XMLUtils.JAXBMarshalling(listPlaces, realPath + resultFile);
            response.sendRedirect("PlaceListSearch.jsp?txtResultFile=" + resultFile);
            return;
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ListPlaceSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ListPlaceSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(ListPlaceSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void searchDOM(Node node, String searchText) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("placeInList")) {
            NodeList childNodes = node.getChildNodes();
            int id = 0;
            String name = "";
            String picturePath = "";
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node n = childNodes.item(i);
                if (n.getNodeName().equals("id")) {
                    id = Integer.parseInt(n.getTextContent());
                }
                if (n.getNodeName().equals("name")) {
                    name = n.getTextContent();
                    if (name.toLowerCase().indexOf(searchText.toLowerCase()) == -1) {
                        break;
                    }
                }
                if (n.getNodeName().equals("picturePath")) {
                    picturePath = n.getTextContent();
                    PlaceInList pil = new PlaceInList(id, name, picturePath);
                    listPlaces.getPlaceInList().add(pil);
                }
            }
        }
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            searchDOM(childNodes.item(i), searchText);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
